package com.my.roco.service.impl;

import com.my.roco.dao.PetMapper;
import com.my.roco.pojo.Pet;
import com.my.roco.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetServiceImpl implements PetService {

    @Autowired
    private PetMapper petMapper;

    @Override
    public void addPet(Pet pet) {
        if (existsByName(pet.getName())) {
            throw new IllegalArgumentException("宠物名称已存在");
        }
        if (existsByNumber(pet.getNumber())) {
            throw new IllegalArgumentException("宠物编号已存在");
        }
        petMapper.insertPet(pet);
    }

    @Override
    public Pet getPetByNumber(String number) {
        return petMapper.getPetByNumber(number);
    }

    @Override
    public List<Pet> getPetsByAttribute(String attribute) {
        return petMapper.getPetsByAttribute(attribute);
    }

    @Override
    public List<Pet> getAllPets() {
        return petMapper.getAllPets();
    }
    @Override
    public List<Pet> searchPets(String keyword) {
        return petMapper.searchPets(keyword);
    }
    
    @Override
    public boolean existsByName(String name) {
        return petMapper.getPetByName(name) != null;
    }
    
    @Override
    public boolean existsByNumber(String number) {
        return petMapper.getPetByNumber(number) != null;
    }
    
    @Override
    public void deletePet(String number) {
        petMapper.deletePet(number);
    }
    
    @Override
    public List<Pet> getPetsByCondition(
        String keyword, 
        String attribute, 
        String sortField, 
        String sortOrder
    ) {
        return petMapper.getPetsByCondition(keyword, attribute, sortField, sortOrder);
    }
}